from flask import Flask, jsonify, request
from flask_cors import CORS  # 导入CORS模块

app = Flask(__name__)
CORS(app)  # 启用CORS支持，解决跨域问题

# 模拟数据 - 年度GDP数据
annual_gdp_data = {
    "year": [2019, 2020, 2021, 2022, 2023, 2024],
    "gdp_total": [54259.2, 54997.1, 58887.4, 61345.1, 61327.5, 63589.99],
    "growth_rate": [7.0, 1.3, 6.3, 4.1, 6.2, 5.1],
    "gdp_per_capita": [55000, 56000, 58000, 60000, 62148, 64210],
    "primary_perc": [8.5, 9.7, 9.1, 9.0, 8.7, 8.6],
    "secondary_perc": [43.5, 41.7, 41.3, 41.5, 41.8, 38.3],
    "tertiary_perc": [48.0, 48.6, 49.6, 49.5, 49.5, 53.1]
}

# 模拟数据 - 区域GDP数据
regional_gdp_data = [
    {"city": "郑州", "gdp_value": 14200, "growth_rate": 6.8, "province_rank": 1},
    {"city": "洛阳", "gdp_value": 6000, "growth_rate": 6.5, "province_rank": 2},
    {"city": "南阳", "gdp_value": 4800, "growth_rate": 6.3, "province_rank": 3},
    {"city": "许昌", "gdp_value": 4200, "growth_rate": 6.0, "province_rank": 4},
    {"city": "周口", "gdp_value": 4100, "growth_rate": 6.5, "province_rank": 5},
    {"city": "新乡", "gdp_value": 3800, "growth_rate": 6.2, "province_rank": 6},
    {"city": "商丘", "gdp_value": 3600, "growth_rate": 6.4, "province_rank": 7},
    {"city": "驻马店", "gdp_value": 3500, "growth_rate": 6.3, "province_rank": 8},
    {"city": "信阳", "gdp_value": 3400, "growth_rate": 6.1, "province_rank": 9},
    {"city": "平顶山", "gdp_value": 3200, "growth_rate": 6.0, "province_rank": 10}
]

# 定义路由 - 获取年度GDP数据
@app.route('/api/annual_gdp', methods=['GET'])
def get_annual_gdp():
    """返回年度GDP数据"""
    return jsonify({
        "status": "success",
        "message": "河南省年度GDP数据获取成功",
        "data": annual_gdp_data
    })

# 定义路由 - 获取区域GDP数据
@app.route('/api/regional_gdp', methods=['GET'])
def get_regional_gdp():
    """返回区域GDP数据"""
    # 获取查询参数中的年份
    year = request.args.get('year')
    
    # 如果提供了年份参数
    if year:
        # 在实际项目中，这里会根据年份从数据库获取数据
        # 这里简化处理，返回过滤后的数据
        return jsonify({
            "status": "success",
            "message": f"河南省{year}年区域GDP数据获取成功",
            "data": regional_gdp_data
        })
    
    # 没有提供年份参数，返回所有数据
    return jsonify({
        "status": "success",
        "message": "河南省区域GDP数据获取成功",
        "data": regional_gdp_data
    })

# 定义路由 - 获取产业结构数据
@app.route('/api/industry_structure', methods=['GET'])
def get_industry_structure():
    """返回产业结构数据"""
    year = request.args.get('year')
    
    if year:
        # 在实际项目中，这里会根据年份从数据库获取数据
        return jsonify({
            "status": "success",
            "message": f"河南省{year}年产业结构数据获取成功",
            "data": annual_gdp_data  # 使用同一个数据结构
        })
    
    return jsonify({
        "status": "success",
        "message": "河南省产业结构数据获取成功",
        "data": {
            "year": annual_gdp_data["year"],
            "primary": annual_gdp_data["primary_perc"],
            "secondary": annual_gdp_data["secondary_perc"],
            "tertiary": annual_gdp_data["tertiary_perc"]
        }
    })

# 运行Flask应用
if __name__ == '__main__':
    # 启动服务器，0.0.0.0表示监听所有网络接口，5000是端口号
    app.run(host='0.0.0.0', port=5000, debug=True)